package com.zyj.blog.service.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zyj.blog.pojo.SysUserExample;
import com.zyj.blog.pojo.SysUserRole;
import com.zyj.blog.mapper.SysRoleMapper;
import com.zyj.blog.mapper.SysUserMapper;
import com.zyj.blog.mapper.SysUserRoleMapper;
import com.zyj.blog.page.ColumnFilter;
import com.zyj.blog.page.MybatisPageHelper;
import com.zyj.blog.page.PageRequest;
import com.zyj.blog.page.PageResult;
import com.zyj.blog.pojo.SysMenu;
import com.zyj.blog.pojo.SysRole;
import com.zyj.blog.pojo.SysUser;
import com.zyj.blog.service.SysMenuService;
import com.zyj.blog.service.SysUserService;

/**
 * @author zyj
 *
 */
@Service
public class SysUserServiceImpl implements SysUserService {
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	@Autowired
	private SysMenuService sysMenuService;
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;

	/* 
	 * 通过用户id查询用户
	 */
	@Override
	public SysUser findById(Long userId) {
		return sysUserMapper.selectByPrimaryKey(userId);
	}

	/* 
	 * 通过用户名查找用户
	 */
	@Override
	public SysUser findByUserName(String username) {
		SysUserExample example = new SysUserExample();
		example.createCriteria().andNameEqualTo(username);
		List<SysUser> users = sysUserMapper.selectByExample(example);
		if(users.isEmpty()) {
			return null;
		}
		return users.get(0);
	}

	/* 
	 * 通过用户名查看权限
	 */
	@Override
	public Set<String> findPermissions(String username) {
		Set<String> perms = new HashSet<>();
		List<SysMenu> sysMenus = sysMenuService.findByUser(username);
		for(SysMenu sysMenu:sysMenus) {
			perms.add(sysMenu.getPerms());
		}
		return perms;
	}

	/* (non-Javadoc)
	 * @see com.zyj.blog.service.CurdService#save(java.lang.Object)
	 */
	@Override
	public int save(SysUser record) {
		Long id = null;
		if(record.getId() == null || record.getId() == 0) {
			// 新增用户
			sysUserMapper.insertSelective(record);
			id = record.getId();
		} else {
			// 更新用户信息
			sysUserMapper.updateByPrimaryKeySelective(record);
		}
		// 更新用户角色
		if(id != null && id == 0) {
			return 1;
		}
		if(id != null) {
			for(SysUserRole sysUserRole:record.getUserRoles()) {
				sysUserRole.setUserId(id);
			}
		} else {
			sysUserRoleMapper.deleteByUserId(record.getId());
		}
		for(SysUserRole sysUserRole:record.getUserRoles()) {
			sysUserRoleMapper.insertSelective(sysUserRole);
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see com.zyj.blog.service.CurdService#delete(java.lang.Object)
	 */
	@Override
	public int delete(SysUser record) {
		return sysUserMapper.deleteByPrimaryKey(record.getId());
	}

	/* (non-Javadoc)
	 * @see com.zyj.blog.service.CurdService#delete(java.util.List)
	 */
	@Override
	public int delete(List<SysUser> records) {
		for(SysUser record:records) {
			delete(record);
		}
		return 1;
	}

	/* 
	 * 
	 */
	@Override
	public PageResult findPage(PageRequest pageRequest) {
		PageResult pageResult = null;
		ColumnFilter columnFilter = pageRequest.getColumnFilter("name");
		if(columnFilter != null && columnFilter.getValue() != null) {
			pageResult = MybatisPageHelper.findPage(pageRequest, sysUserMapper, "findPageByName", columnFilter.getValue());
		} else {
			pageResult = MybatisPageHelper.findPage(pageRequest, sysUserMapper);
		}
		// 加载用户角色信息
		findUserRoles(pageResult);
		return pageResult;
	}

	/**
	 * @param pageResult
	 */
	private void findUserRoles(PageResult pageResult) {
		List<?> content = pageResult.getContent();
		for(Object object:content) {
			SysUser sysUser = (SysUser) object;
			List<SysUserRole> userRoles = findUserRoles(sysUser.getId());
			sysUser.setUserRoles(userRoles);
			sysUser.setRoleNames(getRoleNames(userRoles));
		}
		
	}

	/**
	 * @param userRoles
	 * @return
	 */
	private String getRoleNames(List<SysUserRole> userRoles) {
		StringBuilder sb = new StringBuilder();
		for(Iterator<SysUserRole> iter=userRoles.iterator(); iter.hasNext();) {
			SysUserRole userRole = iter.next();
			SysRole sysRole = sysRoleMapper.selectByPrimaryKey(userRole.getRoleId());
			if(sysRole == null) {
				continue ;
			}
			sb.append(sysRole.getRemark());
			if(iter.hasNext()) {
				sb.append(", ");
			}
		}
		return sb.toString();
	}

	/**
	 * @param id
	 * @return
	 */
	private List<SysUserRole> findUserRoles(Long userId) {
		return sysUserRoleMapper.findUserRoles(userId);
	}

}
